package com.b2.service.impl;

import com.b2.constant.MessageConstant;
import com.b2.mapper.BookMapper;
import com.b2.pojo.dto.AbilityDto;
import com.b2.pojo.entity.Book;
import com.b2.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.b2.pojo.entity.Ability;
import com.b2.service.AbilityService;
import com.b2.mapper.AbilityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author 悦神
* @description 针对表【ability】的数据库操作Service实现
* @createDate 2024-05-27 10:28:35
*/
@Service
public class AbilityServiceImpl extends ServiceImpl<AbilityMapper, Ability>
    implements AbilityService{

    @Autowired
    AbilityMapper abilityMapper;
    @Autowired
    BookMapper bookMapper;
    @Override
    public Result<List<Ability>> check(Long id) {
        LambdaQueryWrapper<Ability> abilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        abilityLambdaQueryWrapper.eq(Ability::getBookId,id);
        List<Ability> abilities = abilityMapper.selectList(abilityLambdaQueryWrapper);
        return Result.success(abilities);
    }
    @Transactional
    @Override
    public Result delete(Long id) {

        abilityMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public Result add(AbilityDto abilityDto) {

        String regex7 = "^[A-Z]{4}[0-9]{2}[A-Z]$";//长度为7，第56为数字
        //判断能力编码是否重复
        LambdaQueryWrapper<Ability> o = new LambdaQueryWrapper<>();
        o.eq(Ability::getAbilityCode,abilityDto.getAbilityCode());
        List<Ability> abilities = abilityMapper.selectList(o);
        if (abilities.size()>0) {
            throw new RuntimeException(MessageConstant.ABILITY_CODE_EXIST);
        }

        //判断能力名字是否重复
      /*  LambdaQueryWrapper<Ability> o1 = new LambdaQueryWrapper<>();
        o1.eq(Ability::getAbilityDescribution,abilityDto.getAbilityDescribution());
        List<Ability> abilities1 = abilityMapper.selectList(o1);
        if (abilities1.size()>0)
            throw new RuntimeException(MessageConstant.ABILITY_DESCRIBUTION_EXIST);*/

        //判断绘本编码是否符合要求
        Pattern compile = Pattern.compile(regex7);
        Matcher matcher = compile.matcher(abilityDto.getAbilityCode());
        if (!matcher.matches()) {
            throw new RuntimeException(MessageConstant.ABILITY_CODE_FAIL);
        }
        Ability ability = new Ability();
        ability.setBookId(abilityDto.getBookId());
        ability.setAbilityCode(abilityDto.getAbilityCode());
        ability.setAbilityDescribution(abilityDto.getAbilityDescribution());
        abilityMapper.insert(ability);
        return Result.success();
    }

    @Override
    public String[] getInformation(String[] abilityCodes) {
        String[] informations = new String[abilityCodes.length];

        for (int i = 0; i <abilityCodes.length ; i++) {
            char[] chars = abilityCodes[i].toCharArray();
            if (chars[4]=='O'){
                chars[4]='0';
            }
            if (chars[5]=='O'){
                chars[5]='0';
            }
            abilityCodes[i]=new String(chars);
            LambdaQueryWrapper<Ability> abilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<Ability> eq = abilityLambdaQueryWrapper.eq(Ability::getAbilityCode, abilityCodes[i]);
            Ability ability = abilityMapper.selectOne(eq);
            if (ability==null){
                informations[i]=null;
               // throw new RuntimeException(MessageConstant.ABILITY_CODE_NOT_EXIST);
            }
          else {
                informations[i]=ability.getAbilityDescribution();
            }
        }


        return informations ;
    }

    @Override
    public String getname(String abilitycode) {
        char[] chars = abilitycode.toCharArray();
        if (chars[4]=='O'){
            chars[4]='0';
        }
        if (chars[5]=='O'){
            chars[5]='0';
        }
        abilitycode=new String(chars);
        LambdaQueryWrapper<Ability> abilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Ability> eq = abilityLambdaQueryWrapper.eq(Ability::getAbilityCode, abilitycode);
        Ability ability = abilityMapper.selectOne(eq);
        Book book = bookMapper.selectById(ability.getBookId());
        return book.getBookName();
    }
}




